<!-- build time:Tue Dec 17 2019 19:26:50 GMT+0800 (GMT+08:00) --><!doctype html><html class="theme-next mist" lang="zh-Hans"><head><meta name="generator" content="Hexo 3.8.0"><meta name="google-site-verification" content="7Tau9WyVgxnsEY9oYedu9g0U6_8akOX3wiKbaYcrg9A"><meta name="baidu-site-verification" content="EVwLiaxdxX"><link href="/css/xps13.css" rel="stylesheet" type="text/css"><link href="/css/message.css" rel="stylesheet" type="text/css"><script type="text/javascript" src="/js/jquery-1.11.3.min.js"></script><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css"><link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css"><meta name="keywords" content="Java,"><link rel="alternate" href="/atom.xml" title="MrBird" type="application/atom+xml"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1"><meta name="description" content="附件上传下载的基本过程主要包含两个步骤：1.通过IO流将附件上传/下载到对应的文件夹内；2.将附件对应的信息（经过处理后的附件名，格式，大小等）保存到数据库。下图是此次demo的效果图（默认用户id为1）："><meta name="keywords" content="Java"><meta property="og:type" content="article"><meta property="og:title" content="上传下载demo"><meta property="og:url" content="http://mrbird.cc/上传下载demo.html"><meta property="og:site_name" content="MrBird"><meta property="og:description" content="附件上传下载的基本过程主要包含两个步骤：1.通过IO流将附件上传/下载到对应的文件夹内；2.将附件对应的信息（经过处理后的附件名，格式，大小等）保存到数据库。下图是此次demo的效果图（默认用户id为1）："><meta property="og:locale" content="zh-Hans"><meta property="og:image" content="http://mrbird.cc/img/72187635-file_1487994298797_5113.gif"><meta property="og:updated_time" content="2019-10-28T12:14:46.308Z"><meta name="twitter:card" content="summary"><meta name="twitter:title" content="上传下载demo"><meta name="twitter:description" content="附件上传下载的基本过程主要包含两个步骤：1.通过IO流将附件上传/下载到对应的文件夹内；2.将附件对应的信息（经过处理后的附件名，格式，大小等）保存到数据库。下图是此次demo的效果图（默认用户id为1）："><meta name="twitter:image" content="http://mrbird.cc/img/72187635-file_1487994298797_5113.gif"><script type="text/javascript" id="hexo.configurations">var NexT=window.NexT||{},CONFIG={root:"/",scheme:"Mist",sidebar:{position:"left",display:"always",offset:12,offset_float:0,b2t:!1,scrollpercent:!1},fancybox:!1,motion:!1}</script><title>上传下载demo | MrBird</title></head><body ondragstart="return!1" class="animsition" lang="zh-Hans" style="overflow-x:hidden;padding-right:280px"><script type="text/javascript" src="/js/mo.min.js"></script><style>@media (min-width:768px) and (max-width:991px){body .header-innerr{width:700px!important}}.header-innerr{margin:0 auto;padding:100px 0 70px;width:880px}@media (min-width:1600px){.container .header-innerr{width:1200px}}.header-innerr{position:relative}.header-innerr{padding:20px 0 0}.header-innerr:after,.header-innerr:before{content:" ";display:table}.header-innerr:after{clear:both}@media (max-width:767px){.header-innerr{width:auto;padding:10px;margin-bottom:-20px}}</style><div class="container sidebar-position-left page-post-detail"><div class="headband"></div><header id="header" class="header"><div class="header-inner"><div class="site-brand-wrapper"><div class="site-meta"><link href="https://fonts.font.im/css?family=Merienda" rel="stylesheet"><div class="custom-logo-site-title"><a href="/" class="brand" rel="start"><span class="logo-line-before"><i></i></span> <span class="site-title" style="font-family:Merienda;font-size:1.3rem">MrBird</span> <span class="logo-line-after"><i></i></span></a></div><p class="site-subtitle"></p></div><div class="site-nav-toggle"><button><span class="btn-bar"></span> <span class="btn-bar"></span> <span class="btn-bar"></span></button></div></div><nav class="site-nav"><ul id="menu" class="menu"><li class="menu-item menu-item-home"><a href="/" rel="section">HOME</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section">ARCHIVES</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section">TAGS</a></li><li class="menu-item menu-item-friends"><a href="/friends/" rel="section">FRIENDS</a></li><div class="sidebar-toggle" style="display:none"><div class="sidebar-toggle-line-wrap"><span class="sidebar-toggle-line sidebar-toggle-line-first"></span> <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span> <span class="sidebar-toggle-line sidebar-toggle-line-last"></span></div></div></ul><div class="site-search"><div class="popup search-popup local-search-popup"><div class="local-search-header clearfix"><span class="search-icon"><i class="fa fa-search"></i> </span><span class="popup-btn-close"><i class="fa fa-times-circle"></i></span><div class="local-search-input-wrapper"><input autocomplete="off" placeholder="Search" spellcheck="false" type="text" id="local-search-input"></div></div><div id="local-search-result"></div></div></div></nav></div><div class="header-innerr"><div class="note info" style="margin:0;letter-spacing:.15px">🐤手把手教你搭建<strong>Spring Cloud微服务权限系统</strong>（从零到部署）：<a style="color:#40dab2;font-weight:600" href="https://www.kancloud.cn/mrbird/spring-cloud" target="_blank">https://www.kancloud.cn/mrbird/spring-cloud</a></div></div></header><main id="main" class="main"><div class="main-inner"><div class="content-wrap"><div id="content" class="content"><div id="posts" class="posts-expand"><article class="post post-type-normal" itemscope itemtype="http://schema.org/Article"><link itemprop="mainEntityOfPage" href="http://mrbird.cc/上传下载demo.html"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="name" content="MrBird"><meta itemprop="description" content=""><meta itemprop="image" content="/images/blogImage.jpg"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="MrBird"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">上传下载demo</h1><div class="post-meta"><span class="post-time"><span class="post-meta-item-icon"><i class="fa fa-calendar-o"></i> </span><span class="post-meta-item-text">Posted on</span> <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-04-01T09:19:17+08:00">2016-04-01 </time></span><span></span> <span class="post-meta-divider">|</span> <span class="page-pv"><i class="fa fa-laptop"></i>&nbsp;&nbsp;Visit count <span class="busuanzi-value" id="busuanzi_value_page_pv"></span></span></div></header><div class="post-body" itemprop="articleBody"><p>附件上传下载的基本过程主要包含两个步骤：</p><p>1.通过IO流将附件上传/下载到对应的文件夹内；</p><p>2.将附件对应的信息（经过处理后的附件名，格式，大小等）保存到数据库。</p><p>下图是此次demo的效果图（默认用户id为1）：<a id="more"></a></p><p><img src="img/72187635-file_1487994298797_5113.gif" alt="72187635-file_1487994298797_5113.gif"></p><p>数据库附件表SYS_ANNEX：</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">create</span> <span class="keyword">table</span> SYS_ANNEX  (</span><br><span class="line">   ANNEX_ID             VARCHAR2(<span class="number">20</span>)         <span class="keyword">not</span> <span class="literal">null</span>, <span class="comment">--附件id</span></span><br><span class="line">   AREA_ID              <span class="built_in">NUMBER</span>(<span class="number">5</span>),                                  </span><br><span class="line">   BUSI_TYPE_ID         <span class="built_in">NUMBER</span>(<span class="number">5</span>),           <span class="comment">--业务类型id，这里我们默认为1</span></span><br><span class="line">   ANNEX_KEY            VARCHAR2(<span class="number">20</span>)         <span class="keyword">not</span> <span class="literal">null</span>,   </span><br><span class="line">   <span class="keyword">NAME</span>                 VARCHAR2(<span class="number">100</span>)        <span class="keyword">not</span> <span class="literal">null</span>, <span class="comment">--附件名</span></span><br><span class="line">   USER_ID              <span class="built_in">NUMBER</span>(<span class="number">12</span>)           <span class="keyword">not</span> <span class="literal">null</span>, <span class="comment">--用户id，这里我们默认为1</span></span><br><span class="line">   CREATE_DATE          <span class="built_in">DATE</span>                 <span class="keyword">not</span> <span class="literal">null</span>, <span class="comment">--创建时间</span></span><br><span class="line">   STATE_ID             <span class="built_in">NUMBER</span>(<span class="number">5</span>)            <span class="keyword">not</span> <span class="literal">null</span>,</span><br><span class="line">   STATE_DATE           <span class="built_in">DATE</span>                 <span class="keyword">not</span> <span class="literal">null</span>,</span><br><span class="line">   <span class="keyword">constraint</span> PK_SYS_ANNEX primary <span class="keyword">key</span> (ANNEX_ID)</span><br><span class="line">);</span><br></pre></td></tr></table></figure><p></p><p>Session工具类，用来创建查询：</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SessionUtil</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> SessionFactory sf;</span><br><span class="line">    <span class="keyword">static</span>&#123;</span><br><span class="line">    	Configuration conf=<span class="keyword">new</span> Configuration();</span><br><span class="line">    	conf.configure(<span class="string">"hibernate.cfg.xml"</span>);</span><br><span class="line">    	sf=conf.buildSessionFactory();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Session <span class="title">getSession</span><span class="params">()</span></span>&#123;</span><br><span class="line">    	<span class="keyword">return</span> sf.openSession();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> SessionFactory <span class="title">getSf</span><span class="params">()</span></span>&#123;</span><br><span class="line">    	<span class="keyword">return</span> sf;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p>上传模块servlet：</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UploadHandleServlet</span> <span class="keyword">extends</span> <span class="title">HttpServlet</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">service</span><span class="params">(HttpServletRequest request, HttpServletResponse response)</span></span></span><br><span class="line"><span class="function">  <span class="keyword">throws</span> ServletException, IOException </span>&#123;</span><br><span class="line">    <span class="comment">// 得到上传文件的保存目录，将上传的文件存放于WEB-INF目录下，不允许外界直接访问，</span></span><br><span class="line">    <span class="comment">// 保证上传文件的安全</span></span><br><span class="line">    String savePath = <span class="keyword">this</span>.getServletContext().getRealPath(<span class="string">"/WEB-INF/upload"</span>);</span><br><span class="line">    <span class="comment">// 上传时生成的临时文件保存目录</span></span><br><span class="line">    String tempPath = <span class="keyword">this</span>.getServletContext().getRealPath(<span class="string">"/WEB-INF/temp"</span>);</span><br><span class="line">    File tmpFile = <span class="keyword">new</span> File(tempPath);</span><br><span class="line">    <span class="keyword">if</span> (!tmpFile.exists()) &#123;</span><br><span class="line">    	<span class="comment">// 创建临时目录</span></span><br><span class="line">    	tmpFile.mkdir();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 消息提示</span></span><br><span class="line">    String message = <span class="string">""</span>;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="comment">// 使用Apache文件上传组件处理文件上传步骤：</span></span><br><span class="line">        <span class="comment">// 1、创建一个DiskFileItemFactory工厂</span></span><br><span class="line">        DiskFileItemFactory factory = <span class="keyword">new</span> DiskFileItemFactory();</span><br><span class="line">        <span class="comment">// 设置工厂的缓冲区的大小，当上传的文件大小超过缓冲区的大小时，就会生成一个</span></span><br><span class="line">        <span class="comment">// 临时文件存放到指定的临时目录当中。设置缓冲区的大小为100KB，如果不指定，</span></span><br><span class="line">        <span class="comment">// 那么缓冲区的大小默认是10KB</span></span><br><span class="line">        factory.setSizeThreshold(<span class="number">1024</span> * <span class="number">100</span>);</span><br><span class="line">        <span class="comment">// 设置上传时生成的临时文件的保存目录</span></span><br><span class="line">        factory.setRepository(tmpFile);</span><br><span class="line">        <span class="comment">// 2、创建一个文件上传解析器</span></span><br><span class="line">        ServletFileUpload upload=<span class="keyword">new</span> ServletFileUpload(factory);</span><br><span class="line">        <span class="comment">// 解决上传文件名的中文乱码</span></span><br><span class="line">        upload.setHeaderEncoding(<span class="string">"UTF-8"</span>);</span><br><span class="line">        <span class="comment">// 3、判断提交上来的数据是否是上传表单的数据</span></span><br><span class="line">        <span class="keyword">if</span> (!ServletFileUpload.isMultipartContent(request)) &#123;</span><br><span class="line">        	<span class="comment">// 按照传统方式获取数据</span></span><br><span class="line">        	<span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 设置上传单个文件的大小的最大值，目前是设置为1024*1024*100字节，</span></span><br><span class="line">        <span class="comment">// 也就是100MB</span></span><br><span class="line">        upload.setFileSizeMax(<span class="number">1024</span> * <span class="number">1024</span> * <span class="number">100</span>);</span><br><span class="line">        <span class="comment">// 设置上传文件总量的最大值，最大值=同时上传的多个文件的大小的</span></span><br><span class="line">        <span class="comment">// 最大值的和，目前设置为1000MB</span></span><br><span class="line">        upload.setSizeMax(<span class="number">1024</span> * <span class="number">1024</span> * <span class="number">1000</span>);</span><br><span class="line">        <span class="comment">// 4、使用ServletFileUpload解析器解析上传数据，解析结果返回的</span></span><br><span class="line">        <span class="comment">// 是一个List&lt;FileItem&gt;集合，每一个FileItem对应一个Form表单的</span></span><br><span class="line">        <span class="comment">// 输入项</span></span><br><span class="line">        List&lt;FileItem&gt; list = upload.parseRequest(request);</span><br><span class="line">        <span class="keyword">for</span> (FileItem item : list) &#123;</span><br><span class="line">        	<span class="comment">// 如果fileitem中封装的是普通输入项的数据</span></span><br><span class="line">        	<span class="keyword">if</span> (item.isFormField()) &#123;</span><br><span class="line">            	String name = item.getFieldName();</span><br><span class="line">            	<span class="comment">// 解决普通输入项的数据的中文乱码问题</span></span><br><span class="line">            	String value = item.getString(<span class="string">"UTF-8"</span>);					</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;<span class="comment">// 如果fileitem中封装的是上传文件</span></span><br><span class="line">            	<span class="comment">// 得到上传的文件名称，</span></span><br><span class="line">            	String filename = item.getName();</span><br><span class="line">            	System.out.println(filename);</span><br><span class="line">            	<span class="keyword">if</span> (filename == <span class="keyword">null</span> || </span><br><span class="line">            	filename.trim().equals(<span class="string">""</span>)) &#123;</span><br><span class="line">            		<span class="keyword">continue</span>;</span><br><span class="line">            	&#125;</span><br><span class="line">                <span class="comment">// 注意：不同的浏览器提交的文件名是不一样的，有些浏览器提交上来的</span></span><br><span class="line">                <span class="comment">// 文件名是带有路径的，如： c:\a\b\1.txt，而有些只是单纯的文件名，</span></span><br><span class="line">                <span class="comment">// 如：1.txt，处理获取到的上传文件的文件名的路径部分，只保留文件</span></span><br><span class="line">                <span class="comment">// 名部分</span></span><br><span class="line">                filename = filename.substring(filename.lastIndexOf(<span class="string">"\\"</span>) + <span class="number">1</span>);</span><br><span class="line">                <span class="comment">// 得到上传文件的扩展名</span></span><br><span class="line">                String fileExtName</span><br><span class="line">                    = filename.substring(filename.lastIndexOf(<span class="string">"."</span>) + <span class="number">1</span>);</span><br><span class="line">                <span class="comment">// 如果需要限制上传的文件类型，那么可以通过文件的扩展名来判断上传的</span></span><br><span class="line">                <span class="comment">// 文件类型是否合法</span></span><br><span class="line">                System.out.println( <span class="string">"上传的文件的扩展名是："</span> + fileExtName);</span><br><span class="line">                <span class="comment">// 获取item中的上传文件的输入流</span></span><br><span class="line">                InputStream in = item.getInputStream();</span><br><span class="line">                <span class="comment">// 得到文件保存的名称</span></span><br><span class="line">                String saveFilename = makeFileName(filename);</span><br><span class="line">                <span class="comment">// 得到文件的保存目录</span></span><br><span class="line">                String realSavePath = savePath;</span><br><span class="line">                <span class="comment">// 创建一个文件输出流</span></span><br><span class="line">                FileOutputStream out </span><br><span class="line">                    = <span class="keyword">new</span> FileOutputStream(realSavePath+ <span class="string">"\\"</span>+ saveFilename);</span><br><span class="line">                <span class="comment">// 创建一个缓冲区</span></span><br><span class="line">                <span class="keyword">byte</span> buffer[] = <span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">1024</span>];</span><br><span class="line">                <span class="comment">// 判断输入流中的数据是否已经读完的标识</span></span><br><span class="line">                <span class="keyword">int</span> len = <span class="number">0</span>;</span><br><span class="line">                <span class="comment">// 循环将输入流读入到缓冲区当中，(len=in.read(buffer))&gt;0就</span></span><br><span class="line">                <span class="comment">// 表示in里面还有数据</span></span><br><span class="line">                <span class="keyword">while</span> ((len = in.read(buffer)) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                	<span class="comment">// 使用FileOutputStream输出流将缓冲区的数据写入到指定的</span></span><br><span class="line">                	<span class="comment">// 目录(savePath + "\\"+ filename)中</span></span><br><span class="line">                	out.write(buffer, <span class="number">0</span>, len);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">// 关闭输入流</span></span><br><span class="line">                in.close();</span><br><span class="line">                <span class="comment">// 关闭输出流</span></span><br><span class="line">            	out.close(); </span><br><span class="line">            	<span class="comment">// 删除处理文件上传时生成的临时文件 </span></span><br><span class="line">            	item.delete();</span><br><span class="line">            	message = <span class="string">"文件上传成功！"</span>;</span><br><span class="line">            	<span class="comment">// 文件上传成功后，将对应的信息保存到数据库SYS_ANNEX表</span></span><br><span class="line">                Integer userId </span><br><span class="line">                        = Integer.valueOf(request.getParameter(<span class="string">"userId"</span>));</span><br><span class="line">            	Date date = <span class="keyword">new</span> Date(System.currentTimeMillis());</span><br><span class="line">            	String annexId = System.currentTimeMillis() + <span class="string">""</span>;</span><br><span class="line">            	String annexKey = userId + <span class="string">""</span>;</span><br><span class="line">            	Session session = SessionUtil.getSession();</span><br><span class="line">            	String sql = <span class="string">"insert INTO SYS_ANNEX VALUES(ANNEX_SEQ.nextval,"</span>;</span><br><span class="line">                       sql+=<span class="string">"1,1,?,?,?,sysdate,1,sysdate)"</span>;</span><br><span class="line">            	SQLQuery query = session.createSQLQuery(sql);</span><br><span class="line">            	query.setString(<span class="number">0</span>, annexKey);</span><br><span class="line">            	query.setString(<span class="number">1</span>, saveFilename);</span><br><span class="line">            	query.setInteger(<span class="number">2</span>, userId);</span><br><span class="line">            	Transaction ts = session.beginTransaction();</span><br><span class="line">            	<span class="keyword">try</span> &#123;</span><br><span class="line">                    query.executeUpdate();</span><br><span class="line">                    ts.commit();</span><br><span class="line">                    System.out.println(<span class="string">"插入成功"</span>);</span><br><span class="line">            	&#125; <span class="keyword">catch</span> (HibernateException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                    ts.rollback();</span><br><span class="line">                    System.out.println(<span class="string">"插入失败"</span>);</span><br><span class="line">            	&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">                    session.close();</span><br><span class="line">            	&#125;</span><br><span class="line">             &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (FileUploadBase</span><br><span class="line">            .FileSizeLimitExceededException e) &#123;</span><br><span class="line">        	e.printStackTrace();</span><br><span class="line">        	request.setAttribute(</span><br><span class="line">        	    <span class="string">"message"</span>, <span class="string">"单个文件超出最大值！"</span>);</span><br><span class="line">        	<span class="keyword">return</span>;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (FileUploadBase.SizeLimitExceededException e) &#123;</span><br><span class="line">        	e.printStackTrace();</span><br><span class="line">        	request.setAttribute(</span><br><span class="line">        	    <span class="string">"message"</span>,<span class="string">"上传文件的总大小超出限制的最大值！"</span>);</span><br><span class="line">        	<span class="keyword">return</span>;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">        	message = <span class="string">"文件上传失败！"</span>;</span><br><span class="line">        	e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        request.setAttribute(<span class="string">"message"</span>, message);</span><br><span class="line">        &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Method</span>: makeFileName</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Description</span>: 生成上传文件的文件名，文件名以：uuid+"_"+文件的原始名称</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> filename文件的原始名称</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> uuid+"_"+文件的原始名称</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">  <span class="function"><span class="keyword">private</span> String <span class="title">makeFileName</span><span class="params">(String filename)</span> </span>&#123;</span><br><span class="line">     <span class="comment">// 为防止文件覆盖的现象发生，要为上传文件产生一个唯一的文件名</span></span><br><span class="line">     <span class="keyword">return</span> UUID.randomUUID().toString() + <span class="string">"_"</span> + filename;</span><br><span class="line">  &#125;</span><br><span class="line"> </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p>展示当前（用户id为1，业务类型id为1）的所有附件的servlet：</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ListFileServlet</span> <span class="keyword">extends</span> <span class="title">HttpServlet</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">service</span><span class="params">(HttpServletRequest request, HttpServletResponse response)</span></span></span><br><span class="line"><span class="function">  <span class="keyword">throws</span> ServletException, IOException </span>&#123;</span><br><span class="line">        Integer userId = Integer.valueOf(request.getParameter(<span class="string">"userId"</span>));</span><br><span class="line">        Integer busiTypeId </span><br><span class="line">                    = Integer.valueOf(request.getParameter(<span class="string">"busiTypeId"</span>));</span><br><span class="line">        <span class="comment">// 获取上传文件的目录</span></span><br><span class="line">        String uploadFilePath = <span class="keyword">this</span>.getServletContext()</span><br><span class="line">                        .getRealPath(<span class="string">"/WEB-INF/upload"</span>);</span><br><span class="line">        <span class="comment">// 存储要下载的文件名</span></span><br><span class="line">        Map&lt;String, String&gt; fileNameMap = <span class="keyword">new</span> HashMap&lt;String, String&gt;();</span><br><span class="line">        <span class="comment">// 递归遍历filepath目录下的所有文件和目录，将文件的文件名存储到map集合中</span></span><br><span class="line">        listfile(<span class="keyword">new</span> File(uploadFilePath),fileNameMap,userId,busiTypeId);</span><br><span class="line">        <span class="comment">// File既可以代表一个文件也可以代表一个目录</span></span><br><span class="line">        Iterator&lt;Map.Entry&lt;String, String&gt;&gt; it</span><br><span class="line">                        = fileNameMap.entrySet().iterator();</span><br><span class="line">        <span class="keyword">while</span> (it.hasNext()) &#123;</span><br><span class="line">          Map.Entry&lt;String, String&gt; entry = it.next();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 将Map集合发送到listfile.jsp页面进行显示</span></span><br><span class="line">        request.setAttribute(<span class="string">"fileNameMap"</span>, fileNameMap);</span><br><span class="line">        request.getRequestDispatcher(<span class="string">"/listfile.jsp"</span>).forward(request, response);</span><br><span class="line">  &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Method</span>: listfile</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Description</span>: 递归遍历指定目录下的所有文件</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> file：即代表一个文件，也代表一个文件目录</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> map：存储文件名的Map集合</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">listfile</span><span class="params">(File file, Map&lt;String, String&gt; map,Integer userId,</span></span></span><br><span class="line"><span class="function"><span class="params">    Integer busiTypeId)</span></span>&#123;</span><br><span class="line">        Session session = SessionUtil.getSession();</span><br><span class="line">        String sql = <span class="string">"select name from SYS_ANNEX where user_id=?"</span></span><br><span class="line">               sql += <span class="string">"and busi_type_id=?"</span>;</span><br><span class="line">        SQLQuery query = session.createSQLQuery(sql);</span><br><span class="line">        query.setInteger(<span class="number">0</span>, userId);</span><br><span class="line">        query.setInteger(<span class="number">1</span>, busiTypeId);</span><br><span class="line">        List&lt;String&gt; list = query.list();</span><br><span class="line">        <span class="keyword">for</span> (String fileName : list) &#123;</span><br><span class="line"> </span><br><span class="line">       <span class="comment">/**</span></span><br><span class="line"><span class="comment">        * 处理文件名，上传后的文件是以uuid_文件名的形式去重新命</span></span><br><span class="line"><span class="comment">        * 名的，去除文件名的uuid_部分file.getName().indexOf("_")</span></span><br><span class="line"><span class="comment">        * 检索字符串中第一次出现"_"字符的位置，如果文件名</span></span><br><span class="line"><span class="comment">        * 类似于：9349249849-88343-8344_多啦A梦.avi那么</span></span><br><span class="line"><span class="comment">        * file.getName().substring(file.getName().indexOf("_")+1)</span></span><br><span class="line"><span class="comment">        * 处理之后就可以得到多啦A梦.avi部分</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">        String realName = fileName.substring(fileName.indexOf(<span class="string">"_"</span>) + <span class="number">1</span>);</span><br><span class="line">        <span class="comment">// file.getName()得到的是文件的原始名称，这个名称是</span></span><br><span class="line">        <span class="comment">// 唯一的，因此可以作为key，realName是处理过后的名称，</span></span><br><span class="line">        <span class="comment">// 有可能会重复</span></span><br><span class="line">        map.put(fileName, realName);</span><br><span class="line">        &#125;</span><br><span class="line">        session.close();</span><br><span class="line">   &#125;</span><br><span class="line"> </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p>下载servlet：</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DownLoadServlet</span> <span class="keyword">extends</span> <span class="title">HttpServlet</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">service</span><span class="params">(HttpServletRequest request, HttpServletResponse response)</span></span></span><br><span class="line"><span class="function">  <span class="keyword">throws</span> ServletException, IOException </span>&#123;</span><br><span class="line">        <span class="comment">// 得到要下载的文件名</span></span><br><span class="line">        String fileName = request.getParameter(<span class="string">"filename"</span>);</span><br><span class="line">        <span class="comment">// 上传的文件都是保存在/WEB-INF/upload目录下的子目录当中</span></span><br><span class="line">        String fileSaveRootPath = <span class="keyword">this</span>.getServletContext()</span><br><span class="line">                                .getRealPath(<span class="string">"/WEB-INF/upload"</span>);</span><br><span class="line">        <span class="comment">// 通过文件名找出文件的所在目录</span></span><br><span class="line">        String path = fileSaveRootPath;</span><br><span class="line">        <span class="comment">// 得到要下载的文件</span></span><br><span class="line">        File file = <span class="keyword">new</span> File(path + <span class="string">"\\"</span> + fileName);</span><br><span class="line">        <span class="comment">// 如果文件不存在</span></span><br><span class="line">        <span class="keyword">if</span> (!file.exists()) &#123;</span><br><span class="line">        	request.setAttribute(<span class="string">"message"</span>, <span class="string">"您要下载的资源已被删除！"</span>);</span><br><span class="line">        	<span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 处理文件名</span></span><br><span class="line">        String realname = fileName.substring(fileName.indexOf(<span class="string">"_"</span>) + <span class="number">1</span>);</span><br><span class="line">        <span class="comment">// 设置响应头，控制浏览器下载该文件</span></span><br><span class="line">        response.setHeader(<span class="string">"content-disposition"</span>, <span class="string">"attachment;filename="</span></span><br><span class="line">        	+ URLEncoder.encode(realname, <span class="string">"UTF-8"</span>));</span><br><span class="line">        <span class="comment">// 读取要下载的文件，保存到文件输入流</span></span><br><span class="line">        FileInputStream in = <span class="keyword">new</span> FileInputStream(path + <span class="string">"\\"</span> + fileName);</span><br><span class="line">        <span class="comment">// 创建输出流</span></span><br><span class="line">        OutputStream out = response.getOutputStream();</span><br><span class="line">        <span class="comment">// 创建缓冲区</span></span><br><span class="line">        <span class="keyword">byte</span> buffer[] = <span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">1024</span>];</span><br><span class="line">        <span class="keyword">int</span> len = <span class="number">0</span>;</span><br><span class="line">        <span class="comment">// 循环将输入流中的内容读取到缓冲区当中</span></span><br><span class="line">        <span class="keyword">while</span> ((len = in.read(buffer)) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        	<span class="comment">// 输出缓冲区的内容到浏览器，实现文件下载</span></span><br><span class="line">        	out.write(buffer, <span class="number">0</span>, len);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 关闭文件输入流</span></span><br><span class="line">        in.close();</span><br><span class="line">        <span class="comment">// 关闭输出流</span></span><br><span class="line">        out.close();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p>删除servlet：</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DeleteFileServlet</span> <span class="keyword">extends</span> <span class="title">HttpServlet</span></span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">service</span><span class="params">(HttpServletRequest request, HttpServletResponse response)</span></span></span><br><span class="line"><span class="function">	<span class="keyword">throws</span> ServletException, IOException </span>&#123;</span><br><span class="line">        <span class="comment">//获取文件名</span></span><br><span class="line">        String fileName = request.getParameter(<span class="string">"fileName"</span>);</span><br><span class="line">        <span class="comment">// 数据库删除记录</span></span><br><span class="line">        Session session=SessionUtil.getSession();</span><br><span class="line">        String sql=<span class="string">"delete from SYS_ANNEX where name=?"</span>;</span><br><span class="line">        SQLQuery query=session.createSQLQuery(sql);</span><br><span class="line">        query.setString(<span class="number">0</span>,fileName);</span><br><span class="line">        Transaction ts=session.beginTransaction();</span><br><span class="line">        <span class="keyword">try</span>&#123;</span><br><span class="line">            query.executeUpdate();</span><br><span class="line">            ts.commit();</span><br><span class="line">        &#125;<span class="keyword">catch</span>(HibernateException e)&#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">            ts.rollback();</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;<span class="keyword">finally</span>&#123;</span><br><span class="line">            session.close();</span><br><span class="line">        &#125;</span><br><span class="line"> </span><br><span class="line">        <span class="comment">// 数据库成功删除记录后，再从硬盘里删除文件</span></span><br><span class="line">        <span class="comment">//上传的文件都是保存在/WEB-INF/upload目录下的子目录当中</span></span><br><span class="line">        String fileSaveRootPath = <span class="keyword">this</span>.getServletContext()</span><br><span class="line">                                .getRealPath(<span class="string">"/WEB-INF/upload"</span>);</span><br><span class="line">        <span class="comment">//通过文件名找出文件的所在目录</span></span><br><span class="line">        String path = fileSaveRootPath;</span><br><span class="line">        <span class="comment">//得到要删除的文件</span></span><br><span class="line">        File file = <span class="keyword">new</span> File(path + <span class="string">"\\"</span> + fileName);</span><br><span class="line">        <span class="comment">//删除文件</span></span><br><span class="line">        file.delete();</span><br><span class="line"> </span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p>上传页面upload.jsp：</p><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">%@</span> <span class="attr">page</span> <span class="attr">language</span>=<span class="string">"java"</span> <span class="attr">import</span>=<span class="string">"java.util.*"</span> <span class="attr">pageEncoding</span>=<span class="string">"UTF-8"</span>%&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">%@taglib</span> <span class="attr">prefix</span>=<span class="string">"c_rt"</span> <span class="attr">uri</span>=<span class="string">"http://java.sun.com/jstl/core_rt"</span> %&gt;</span></span><br><span class="line"> <span class="meta">&lt;!DOCTYPE HTML&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="undefined">    table&#123;</span></span><br><span class="line"><span class="css">        <span class="selector-tag">margin</span><span class="selector-pseudo">:auto</span>;</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined"> </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">title</span>&gt;</span>文件上传<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span> <span class="attr">src</span>=<span class="string">"js/jquery.min.js"</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="javascript">        $(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="comment">//页面一加载调用onLoad()函数读取附件</span></span></span><br><span class="line"><span class="undefined">            onLoad();		   </span></span><br><span class="line"><span class="javascript">            $(<span class="string">"#submit"</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="comment">//将form表单附件格式化</span></span></span><br><span class="line"><span class="javascript">            <span class="keyword">var</span> formData = <span class="keyword">new</span> FormData($(<span class="string">"#form"</span>)[<span class="number">0</span>]);</span></span><br><span class="line"><span class="javascript">            <span class="keyword">var</span> userId=$(<span class="string">"#userId"</span>).val().trim();</span></span><br><span class="line"><span class="javascript">            <span class="keyword">var</span> location=<span class="string">"http://localhost:8080/file/servlet/"</span>;</span></span><br><span class="line"><span class="javascript">            location+=<span class="string">"UploadHandleServlet?userId="</span>+userId;</span></span><br><span class="line"><span class="javascript">            $.ajax(&#123;</span></span><br><span class="line"><span class="javascript">                <span class="comment">//调用上传servlet</span></span></span><br><span class="line"><span class="undefined">                url:location,</span></span><br><span class="line"><span class="javascript">                type:<span class="string">"post"</span>,  </span></span><br><span class="line"><span class="undefined">                data:formData,</span></span><br><span class="line"><span class="javascript">                <span class="keyword">async</span>: <span class="literal">false</span>,  </span></span><br><span class="line"><span class="javascript">                cache: <span class="literal">false</span>,  </span></span><br><span class="line"><span class="javascript">                contentType: <span class="literal">false</span>,  </span></span><br><span class="line"><span class="javascript">                processData: <span class="literal">false</span>,</span></span><br><span class="line"><span class="javascript">                success:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">                    <span class="comment">//成功后清除上传附件模块内容</span></span></span><br><span class="line"><span class="javascript">                    $(<span class="string">"#file"</span>).empty();</span></span><br><span class="line"><span class="undefined">                    AddMore();</span></span><br><span class="line"><span class="javascript">                    <span class="comment">//调用onLoad()函数，刷新</span></span></span><br><span class="line"><span class="undefined">                    onLoad();</span></span><br><span class="line"><span class="undefined">                &#125; </span></span><br><span class="line"><span class="undefined">            &#125;);</span></span><br><span class="line"><span class="undefined">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">//onLoad()函数调用listfileServlet,读取当前用户下的所有附件</span></span></span><br><span class="line"><span class="javascript">        <span class="function"><span class="keyword">function</span> <span class="title">onLoad</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">        $.ajax(&#123;</span></span><br><span class="line"><span class="javascript">            url:<span class="string">"http://localhost:8080/file/listfile"</span>,</span></span><br><span class="line"><span class="javascript">            type:<span class="string">"post"</span>,</span></span><br><span class="line"><span class="javascript">            <span class="comment">//模拟用户id为1，业务类型id为1</span></span></span><br><span class="line"><span class="javascript">            data:&#123;<span class="string">"userId"</span>:<span class="number">1</span>,<span class="string">"busiTypeId"</span>:<span class="number">1</span>&#125;,</span></span><br><span class="line"><span class="javascript">            success:<span class="function"><span class="keyword">function</span>(<span class="params">fileNameMap</span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="comment">//将读取到的fileMap展示出来</span></span></span><br><span class="line"><span class="javascript">                $(<span class="string">"#fileLoad"</span>).html(fileNameMap);</span></span><br><span class="line"><span class="undefined">            &#125;</span></span><br><span class="line"><span class="undefined">        &#125;);</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined">&#125;);  </span></span><br><span class="line"><span class="undefined">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="keyword">function</span> <span class="title">AddMore</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> more = <span class="built_in">document</span>.getElementById(<span class="string">"file"</span>);</span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> br = <span class="built_in">document</span>.createElement(<span class="string">"br"</span>);</span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> input = <span class="built_in">document</span>.createElement(<span class="string">"input"</span>);</span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> button = <span class="built_in">document</span>.createElement(<span class="string">"input"</span>);</span></span><br><span class="line"><span class="undefined">       </span></span><br><span class="line"><span class="javascript">        input.type = <span class="string">"file"</span>;</span></span><br><span class="line"><span class="javascript">        input.name = <span class="string">"file"</span>;</span></span><br><span class="line"><span class="javascript">        input.id=<span class="string">"file"</span>;</span></span><br><span class="line"><span class="undefined">       </span></span><br><span class="line"><span class="javascript">        button.type = <span class="string">"button"</span>;</span></span><br><span class="line"><span class="javascript">        button.value = <span class="string">"删除"</span>;</span></span><br><span class="line"><span class="undefined">        </span></span><br><span class="line"><span class="undefined">        more.appendChild(input);</span></span><br><span class="line"><span class="undefined">        more.appendChild(button);</span></span><br><span class="line"><span class="undefined">        more.appendChild(br);</span></span><br><span class="line"><span class="undefined">           </span></span><br><span class="line"><span class="javascript">        button.onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="undefined">        more.removeChild(br);</span></span><br><span class="line"><span class="undefined">        more.removeChild(input);</span></span><br><span class="line"><span class="undefined">        more.removeChild(button);</span></span><br><span class="line"><span class="undefined">    &#125;;</span></span><br><span class="line"><span class="undefined">&#125;</span></span><br><span class="line"><span class="undefined">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">body</span> &gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">form</span> <span class="attr">action</span>=<span class="string">"http://localhost:8080/file/servlet/UploadHandleServlet"</span> </span></span><br><span class="line"><span class="tag">        <span class="attr">enctype</span>=<span class="string">"multipart/form-data"</span> <span class="attr">method</span>=<span class="string">"post"</span> <span class="attr">id</span>=<span class="string">"form"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">table</span> <span class="attr">border</span>=<span class="string">"1"</span> &gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">td</span>&gt;</span>用户ID:<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">td</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">name</span>=<span class="string">"userId"</span> </span></span><br><span class="line"><span class="tag">                       <span class="attr">id</span>=<span class="string">"userId"</span> <span class="attr">value</span>=<span class="string">"1"</span>/&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">td</span>&gt;</span>附件:<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">td</span> <span class="attr">id</span>=<span class="string">"file"</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">td</span> <span class="attr">colspan</span>=<span class="string">"2"</span> <span class="attr">align</span>=<span class="string">"center"</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"增加附件"</span> <span class="attr">onclick</span>=<span class="string">"AddMore()"</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"提交"</span> <span class="attr">id</span>=<span class="string">"submit"</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">td</span>&gt;</span>           </span><br><span class="line">        <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">td</span>&gt;</span>附件下载:<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">td</span> <span class="attr">id</span>=<span class="string">"fileLoad"</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">table</span>&gt;</span>    </span><br><span class="line">   <span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="undefined">        AddMore();</span></span><br><span class="line"><span class="undefined">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure><p></p><p>展示页面listfile.jsp，供ajax调用：</p><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">%@</span> <span class="attr">page</span> <span class="attr">language</span>=<span class="string">"java"</span> <span class="attr">import</span>=<span class="string">"java.util.*"</span> <span class="attr">pageEncoding</span>=<span class="string">"UTF-8"</span>%&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">%@taglib</span> <span class="attr">prefix</span>=<span class="string">"c_rt"</span> <span class="attr">uri</span>=<span class="string">"http://java.sun.com/jstl/core_rt"</span> %&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE HTML&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="css">    <span class="selector-id">#method</span> &#123;</span></span><br><span class="line"><span class="css">        <span class="selector-tag">float</span><span class="selector-pseudo">:right</span>;</span></span><br><span class="line"><span class="css">        <span class="selector-tag">margin-right</span><span class="selector-pseudo">:2px</span>;</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="javascript">    $(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">//删除</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">".delete"</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">var</span> name=$(<span class="keyword">this</span>).attr(<span class="string">"name"</span>);</span></span><br><span class="line"><span class="javascript">            <span class="keyword">var</span> location=<span class="string">"http://localhost:8080/file/servlet/DeleteFileServlet"</span>;</span></span><br><span class="line"><span class="javascript">            $.ajax(&#123;			 </span></span><br><span class="line"><span class="undefined">                url:location,</span></span><br><span class="line"><span class="javascript">                type:<span class="string">"post"</span>, </span></span><br><span class="line"><span class="javascript">                data:&#123;<span class="string">"fileName"</span>:name&#125;,</span></span><br><span class="line"><span class="javascript">                success:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;      			</span></span><br><span class="line"><span class="undefined">                    onLoad();</span></span><br><span class="line"><span class="undefined">                &#125; </span></span><br><span class="line"><span class="undefined">            &#125;);			</span></span><br><span class="line"><span class="undefined">        &#125;);</span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="keyword">function</span> <span class="title">onLoad</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">        $.ajax(&#123;</span></span><br><span class="line"><span class="javascript">            url:<span class="string">"http://localhost:8080/file/listfile"</span>,</span></span><br><span class="line"><span class="javascript">            type:<span class="string">"post"</span>,</span></span><br><span class="line"><span class="javascript">            data:&#123;<span class="string">"userId"</span>:<span class="number">1</span>,<span class="string">"busiTypeId"</span>:<span class="number">1</span>&#125;,</span></span><br><span class="line"><span class="javascript">            success:<span class="function"><span class="keyword">function</span>(<span class="params">fileNameMap</span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">                $(<span class="string">"#fileLoad"</span>).html(fileNameMap);</span></span><br><span class="line"><span class="undefined">            &#125;</span></span><br><span class="line"><span class="undefined">        &#125;);</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined">&#125;);</span></span><br><span class="line"><span class="undefined"> </span></span><br><span class="line"><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>下载文件页面，供ajax返回调用<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 遍历fileNameMap集合 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">c_rt:forEach</span> <span class="attr">var</span>=<span class="string">"me"</span> <span class="attr">items</span>=<span class="string">"$&#123;fileNameMap&#125;"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">c_rt:url</span> <span class="attr">value</span>=<span class="string">"/servlet/DownLoadServlet"</span> <span class="attr">var</span>=<span class="string">"downurl"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">c_rt:param</span> <span class="attr">name</span>=<span class="string">"filename"</span> <span class="attr">value</span>=<span class="string">"$&#123;me.key&#125;"</span>&gt;</span><span class="tag">&lt;/<span class="name">c_rt:param</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">c_rt:url</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">c_rt:url</span> <span class="attr">value</span>=<span class="string">"/servlet/DeleteFileServlet"</span> <span class="attr">var</span>=<span class="string">"deleteurl"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">c_rt:param</span> <span class="attr">name</span>=<span class="string">"filename"</span> <span class="attr">value</span>=<span class="string">"$&#123;me.key&#125;"</span>&gt;</span><span class="tag">&lt;/<span class="name">c_rt:param</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">c_rt:url</span>&gt;</span></span><br><span class="line">    $&#123;me.value&#125;</span><br><span class="line">    <span class="tag">&lt;<span class="name">span</span> <span class="attr">id</span>=<span class="string">"method"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"$&#123;downurl&#125;"</span> <span class="attr">id</span>=<span class="string">"downLoad"</span>&gt;</span>下载<span class="tag">&lt;/<span class="name">a</span>&gt;</span> </span><br><span class="line">        <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"javascript:void(0)"</span> <span class="attr">class</span>=<span class="string">"delete"</span> <span class="attr">name</span>=<span class="string">"$&#123;me.key&#125;"</span>&gt;</span>删除<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">span</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">c_rt:forEach</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure><p></p><p>剩下的只需在web.xml中配置这几个servlet，以及配置hibernate.cfg.xml即可﻿。</p><p>jar包：链接:<a href="http://pan.baidu.com/share/init?shareid=2856097935&amp;uk=20446062" target="_blank" rel="noopener">百度网盘</a> 密码：q75g</p><script>$(".post-body a").not(".thispage").addClass("ignore-href").attr("target","_blank")</script></div><div></div><div><div style="padding:10px 0;margin:20px auto;width:90%;text-align:center;color:#878787"><div>请作者喝瓶肥宅水~</div><button id="rewardButton" style="margin-top:10px" disable="enable" onclick='var e=document.getElementById("QR");"none"===e.style.display?e.style.display="block":e.style.display="none"'><span style="height:46px;width:46px;line-height:46px;border-radius:50%;color:#fe5f55;font-weight:600;background:#ffd5be;border:none;box-shadow:0 4px 8px 0 rgba(255,213,190,.4)">￥</span></button><div id="QR" style="display:none"><div id="wechat" style="display:inline-block"><img id="wechat_qr" src="/img/wechat_pay.png" alt="MrBird WeChat Pay"></div><div id="alipay" style="display:inline-block"><img id="alipay_qr" src="/img/ali_pay.png" alt="MrBird Alipay"></div></div></div><style>#QR img{width:auto;margin:.8em 1em 0 1em}</style></div><div><ul class="post-copyright"><li class="post-copyright-author"><strong>本文作者：</strong> MrBird</li><li class="post-copyright-link"><strong>本文链接：</strong> <a href="http://mrbird.cc/上传下载demo.html" title="上传下载demo">http://mrbird.cc/上传下载demo.html</a></li><li class="post-copyright-license"><strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明出处！</li></ul></div><footer class="post-footer"><div class="post-tags" style="margin-bottom:1.3rem"><a href="/tags/Java/" rel="tag"># Java</a></div><div class="post-nav"><div class="post-nav-next post-nav-item"><a href="/Oracle树查询.html" rel="next" title="Oracle树查询"><i class="fa fa-chevron-left"></i> Oracle树查询</a></div><span class="post-nav-divider"></span><div class="post-nav-prev post-nav-item"><a href="/Apache-POI导出Excel文件.html" rel="prev" title="Apache POI导出Excel文件">Apache POI导出Excel文件 <i class="fa fa-chevron-right"></i></a></div></div></footer></article><hr><div id="container"></div><div class="post-spread"><div id="comment-div"></div><style>.valine .vlist{margin-bottom:3rem}.valine .vwrap .vcontrol .col.col-60{text-align:left}.valine .vlist .vcard .vhead,.valine .vlist .vcard section .vfooter{text-align:left}.valine .vlist .vcard section{padding-bottom:.5rem!important}.vname{color:#42b983!important}.valine .vinfo .col{text-align:left;margin-left:-27rem}div#comment-div{margin-bottom:-8rem}.valine .vlist .vcard .vcontent .code,.valine .vlist .vcard .vcontent code,.valine .vlist .vcard .vcontent pre{background:#fbfbfb}.valine .vlist .vcard .vcontent a{color:#42b983}.valine .vlist .vcard .vimg{border-radius:3px}.valine .vbtn{border-radius:2px;padding:.3rem 1.25rem}.valine .vbtn:active,.valine .vbtn:hover{color:#42b983;border-color:#42b983;background-color:#fff}.valine .vwrap .vheader .vinput:focus{border-bottom-color:#42b983}.valine .vlist .vcard .vcontent.expand:before{background:-webkit-gradient(linear,left top,left bottom,from(hsla(0,0%,100%,0)),to(hsla(0,0%,100%,.2)));background:linear-gradient(180deg,hsla(0,0%,100%,0),hsla(0,0%,100%,.2))}.valine .vlist .vcard .vcontent.expand:after{content:"点击展开";font-size:.4rem;text-align:right;left:-1rem;background:hsla(0,0%,100%,.2)}.valine .vlist .vcard section .vfooter .vat{color:#b3b3b3}.valine .vlist .vcard section .vfooter .vat:hover{color:#42b983}.vcontent img{margin:0}.valine .info{display:none}</style><script type="text/javascript" src="/js/av.min.js"></script><script type="text/javascript" src="/js/Valine.min.js"></script><script type="text/javascript" src="/js/activate-power-mode.js"></script><script>POWERMODE.colorful=!0,POWERMODE.shake=!1,document.body.addEventListener("input",POWERMODE),new Valine({el:"#comment-div",notify:!1,verify:!0,appId:"SMcDFP1bN1jgb9Lo8JmtICHm-gzGzoHsz",appKey:"dH4nrUrt3V5XiJiI6Qyejnbi",placeholder:"",path:window.location.pathname,avatar:"monsterid",guest_info:["nick","mail","link"]});var chicken='<a href="#"><img src="https://image.uisdc.com/wp-content/uploads/2018/06/uisdc-chat-chicken.gif" class="checken"></a>';$("#comment-div").prepend(chicken)</script></div></div><script>var $bqinline=$("img[alt='bq-inline']");$bqinline.css({width:"2.3rem",height:"2.3rem",display:"inline","vertical-align":"text-bottom"})</script></div><div class="comments" id="comments"></div></div><aside id="sidebar" class="sidebar" onselectstart="return!1"><div class="sidebar-inner"><section class="site-overview sidebar-panel sidebar-panel-active"><div class="sidebar-sticky sticky"><div itemscope itemtype="http://schema.org/Person"><div class="author__avatar"><img src="/images/blogImage.jpg" class="author__avatar" alt="MrBird" itemprop="image"></div><div class="author__content"><h3 class="author__name" itemprop="name">MrBird's Blog</h3><p class="author__bio" itemprop="description">A simple blog, code repository, just keep blogging</p></div><div class="author__urls-wrapper"><button class="btn btn--inverse">Follow</button><ul class="author__urls social-icons"><li><a href="http://map.baidu.com/?newmap=1&s=s%26wd%3D%E7%A6%8F%E5%B7%9E%E5%B8%82%26c%3D300&from=alamap&tpl=mapcity" target="_blank" itemprop="url" class="ignore-href"><i class="fa fa-fw fa-map-marker" aria-hidden="true"></i>&nbsp;&nbsp;FuZhou,CN</a></li><li><a href="https://love.mrbird.cc" target="_blank" itemprop="url" class="ignore-href"><i class="fa fa-fw fa-diamond" aria-hidden="true"></i>&nbsp;&nbsp;Love</a></li><li><a href="https://cloud.mrbird.cn" target="_blank" itemprop="url" class="ignore-href"><i class="fa fa-fw fa-chain" aria-hidden="true"></i>&nbsp;&nbsp;FEBS</a></li><li><a href="/atom.xml" target="_blank" itemprop="url" class="ignore-href"><i class="fa fa-fw fa-rss" aria-hidden="true"></i>&nbsp;&nbsp;RSS</a></li><li><a href="https://gitee.com/mrbirdd" target="_blank" itemprop="sameAs" class="ignore-href"><i class="fa fa-fw fa-codepen" aria-hidden="true"></i>&nbsp;&nbsp;Gitee</a></li><li><a href="https://github.com/wuyouzhuguli" target="_blank" itemprop="sameAs" class="ignore-href"><i class="fa fa-fw fa-github-alt" aria-hidden="true"></i>&nbsp;&nbsp;GitHub</a></li><li><a href="javascript:;" class="popup-trigger"><i class="fa fa-fw fa-search" aria-hidden="true"></i>&nbsp;&nbsp;Search</a></li></ul></div></div></div><script>var $urls=$(".author__urls").find("a");$urls.each(function(){var o=$(this);o.mouseenter(function(){o.css({color:"#414547"})}),o.mouseleave(function(){o.css({color:""})})})</script></section></div></aside></div></main><footer id="footer" class="footer" onselectstart="return!1"><div class="footer-inner"><div class="copyright">&copy; 2016 - <span itemprop="copyrightYear">2019</span>&nbsp;&nbsp; <span class="author" itemprop="copyrightHolder">MrBird</span>&nbsp;&nbsp;|<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>&nbsp;&nbsp;UV&nbsp;<span class="busuanzi-value" id="busuanzi_value_site_uv" style="cursor:pointer" title="统计开始时间：2019年7月5日"></span> &nbsp;&nbsp;PV&nbsp;<span class="busuanzi-value" id="busuanzi_value_site_pv" style="cursor:pointer" title="统计开始时间：2019年7月5日"></span></div></div></footer><div class="back-to-top"><span style="font-family:'Source Sans Pro','Helvetica Neue',Arial,sans-serif;font-size:1.2em;font-weight:600">TOP</span></div></div><script type="text/javascript">"[object Function]"!==Object.prototype.toString.call(window.Promise)&&(window.Promise=null)</script><script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script><script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script><script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script><script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script><script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script><script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script><script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script><script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script><script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script><script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script><script type="text/javascript">function proceedsearch(){$("body").append('<div class="search-popup-overlay local-search-pop-overlay"></div>').css("overflow","hidden"),$(".search-popup-overlay").click(onPopupClose),$(".popup").toggle();var t=$("#local-search-input");t.attr("autocapitalize","none"),t.attr("autocorrect","off"),t.focus()}var isfetched=!1,isXml=!0,search_path="search.xml";0===search_path.length?search_path="search.xml":search_path.endsWith("json")&&(isXml=!1);var path="/"+search_path,onPopupClose=function(t){$(".popup").hide(),$("#local-search-input").val(""),$(".search-result-list").remove(),$("#no-result").remove(),$(".local-search-pop-overlay").remove(),$("body").css("overflow","")},searchFunc=function(t,e,o){"use strict";$("body").append('<div class="search-popup-overlay local-search-pop-overlay"><div id="search-loading-icon"><i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i></div></div>').css("overflow","hidden"),$("#search-loading-icon").css("margin","20% auto 0 auto").css("text-align","center"),$.ajax({url:t,dataType:isXml?"xml":"json",async:!0,success:function(t){isfetched=!0,$(".popup").detach().appendTo(".header-inner");var n=isXml?$("entry",t).map(function(){return{title:$("title",this).text(),content:$("content",this).text(),url:$("url",this).text()}}).get():t,r=document.getElementById(e),s=document.getElementById(o),a=function(){var t=r.value.trim().toLowerCase(),e=t.split(/[\s\-]+/);e.length>1&&e.push(t);var o=[];if(t.length>0&&n.forEach(function(n){function r(e,o,n,r){for(var s=r[r.length-1],a=s.position,i=s.word,l=[],h=0;a+i.length<=n&&0!=r.length;){i===t&&h++,l.push({position:a,length:i.length});var p=a+i.length;for(r.pop();0!=r.length&&(s=r[r.length-1],a=s.position,i=s.word,p>a);)r.pop()}return c+=h,{hits:l,start:o,end:n,searchTextCount:h}}function s(t,e){var o="",n=e.start;return e.hits.forEach(function(e){o+=t.substring(n,e.position);var r=e.position+e.length;o+='<b class="search-keyword">'+t.substring(e.position,r)+"</b>",n=r}),o+=t.substring(n,e.end)}var a=!1,i=0,c=0,l=n.title.trim(),h=l.toLowerCase(),p=n.content.trim().replace(/<[^>]+>/g,""),u=p.toLowerCase(),f=decodeURIComponent(n.url),d=[],g=[];if(""!=l&&(e.forEach(function(t){function e(t,e,o){var n=t.length;if(0===n)return[];var r=0,s=[],a=[];for(o||(e=e.toLowerCase(),t=t.toLowerCase());(s=e.indexOf(t,r))>-1;)a.push({position:s,word:t}),r=s+n;return a}d=d.concat(e(t,h,!1)),g=g.concat(e(t,u,!1))}),(d.length>0||g.length>0)&&(a=!0,i=d.length+g.length)),a){[d,g].forEach(function(t){t.sort(function(t,e){return e.position!==t.position?e.position-t.position:t.word.length-e.word.length})});var v=[];0!=d.length&&v.push(r(l,0,l.length,d));for(var C=[];0!=g.length;){var $=g[g.length-1],m=$.position,x=$.word,w=m-20,y=m+80;w<0&&(w=0),y<m+x.length&&(y=m+x.length),y>p.length&&(y=p.length),C.push(r(p,w,y,g))}C.sort(function(t,e){return t.searchTextCount!==e.searchTextCount?e.searchTextCount-t.searchTextCount:t.hits.length!==e.hits.length?e.hits.length-t.hits.length:t.start-e.start});var T=parseInt("1");T>=0&&(C=C.slice(0,T));var b="";b+=0!=v.length?"<li><a href='"+f+"' class='search-result-title'>"+s(l,v[0])+"</a>":"<li><a href='"+f+"' class='search-result-title'>"+l+"</a>",C.forEach(function(t){b+="<a href='"+f+'\'><p class="search-result">'+s(p,t)+"...</p></a>"}),b+="</li>",o.push({item:b,searchTextCount:c,hitCount:i,id:o.length})}}),1===e.length&&""===e[0])s.innerHTML='<div id="no-result"><i class="fa fa-search fa-5x" /></div>';else if(0===o.length)s.innerHTML='<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>';else{o.sort(function(t,e){return t.searchTextCount!==e.searchTextCount?e.searchTextCount-t.searchTextCount:t.hitCount!==e.hitCount?e.hitCount-t.hitCount:e.id-t.id});var a='<ul class="search-result-list">';o.forEach(function(t){a+=t.item}),a+="</ul>",s.innerHTML=a}};r.addEventListener("input",a),$(".local-search-pop-overlay").remove(),$("body").css("overflow",""),proceedsearch()}})};$(".popup-trigger").click(function(t){t.stopPropagation(),isfetched===!1?searchFunc(path,"local-search-input","local-search-result"):proceedsearch()}),$(".popup-btn-close").click(onPopupClose),$(".popup").click(function(t){t.stopPropagation()}),$(document).on("keyup",function(t){var e=27===t.which&&$(".search-popup").is(":visible");e&&onPopupClose()})</script></body><script>$(function(){$("a").not(".nav-link,.cloud-tie-join-count,.ignore-href,.jstree-anchor").addClass("animsition-link")});var burst1=new mojs.Burst({left:0,top:0,radius:{5:40},children:{shape:"circle",fill:["red","cyan","orange"],fillOpacity:.8,radiusX:3.5,radiusY:3.5}});document.addEventListener("click",function(a){null==a.target.href&&"footer"!=a.target.className&&"copyright"!=a.target.className&&"author__urls social-icons"!=a.target.className&&"author__avatar"!=a.target.className&&"sidebar sidebar-active"!=a.target.className&&burst1.tune({x:a.pageX,y:a.pageY}).generate().replay()})</script><script type="text/javascript" src="/js/message.js"></script></html><!-- rebuild by neat -->